package com.yhsoft.buyer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.buyer.entity.BuyerAddress;
import com.yhsoft.buyer.entity.BuyerOrder;
import com.yhsoft.buyer.mapper.BuyerAddressMapper;
import com.yhsoft.buyer.controller.request.BuyerAddressInVo;
import com.yhsoft.buyer.controller.response.BuyerAddressOutVo;
import com.yhsoft.buyer.mapper.BuyerOrderMapper;
import com.yhsoft.buyer.service.IBuyerAddressService;
import com.yhsoft.common.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 地址 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-26
 */
@Service
public class BuyerAddressServiceImpl extends ServiceImpl<BuyerAddressMapper, BuyerAddress> implements IBuyerAddressService {

    @Resource
    private BuyerOrderMapper buyerOrderMapper;

    @Override
    public void addAddress(BuyerAddressInVo inVo) {
        BuyerAddress buyerAddress = new BuyerAddress();
        BeanUtils.copyProperties(inVo, buyerAddress);
        buyerAddress.setUserId(TokenService.getUserBytoken().getId());
        this.baseMapper.insert(buyerAddress);
    }

    @Override
    public String updateAddress(BuyerAddressInVo inVo) {
        // 1. 地址id不存在
        if (inVo.getId() == null || inVo.getId() == 0 ){
            return "订单id不能为空！";
        }

        // 2. 判断地址或者订单是否存在
        BuyerAddress address = this.baseMapper.selectById(inVo.getId());
        if (address == null || StrUtil.isNotBlank(address.getOrderId())){
           return "订单或地址不存在！！";
        }

        // 3. 判断订单是否为待支付
        BuyerOrder buyerOrder = buyerOrderMapper.selectById(address.getOrderId());
        if (buyerOrder.getOrderStatus() != 1){
            return "当前订单状态不是待支付，无法修改！";
        }

        // 4. 根据id修改
        BuyerAddress buyerAddress = new BuyerAddress();
        BeanUtils.copyProperties(inVo, buyerAddress);
        this.baseMapper.updateById(buyerAddress);
        return "success";
    }

    @Override
    public void delBuyerAddress(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<BuyerAddressOutVo> getBuyerAddressList(String userId) {
        LambdaQueryWrapper<BuyerAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BuyerAddress::getUserId, userId);
        queryWrapper.eq(BuyerAddress::getIsShow, 1);
        List<BuyerAddress> buyerAddresses = this.baseMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(buyerAddresses, BuyerAddressOutVo.class);
    }

    @Override
    public void setDefalutAddress(Integer id) {
        LambdaQueryWrapper<BuyerAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BuyerAddress::getUserId, TokenService.getUserBytoken().getId());
        List<BuyerAddress> buyerAddresses = this.baseMapper.selectList(queryWrapper);
        buyerAddresses.forEach(a -> {
                if(a.getId().equals(id)){
                    a.setIsDefault(1);
                    this.baseMapper.updateById(a);
                }else{
                    a.setIsDefault(0);
                    this.baseMapper.updateById(a);
                }
        });
    }

    @Override
    public BuyerAddressOutVo getDefalutAddress() {
        LambdaQueryWrapper<BuyerAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BuyerAddress::getUserId, TokenService.getUserBytoken().getId());
        queryWrapper.eq(BuyerAddress::getIsDefault, 1);
        queryWrapper.eq(BuyerAddress::getIsShow, 1);
        List<BuyerAddress> buyerAddresses = this.baseMapper.selectList(queryWrapper);
        if(buyerAddresses.size() == 0){
            return null;
        }
        return BeanUtil.toBean(buyerAddresses.get(0), BuyerAddressOutVo.class);
    }

    @Override
    public BuyerAddressOutVo getAddress(String orderId) {
        BuyerOrder buyerOrder = this.buyerOrderMapper.selectById(orderId);
        Integer addressId = buyerOrder.getAddressId();
        if ( addressId!= null && addressId != 0) {
            BuyerAddress buyerAddress = this.baseMapper.selectById(addressId);
            BuyerAddressOutVo outVo = new BuyerAddressOutVo();
            BeanUtils.copyProperties(buyerAddress,outVo);
            return outVo;
        }
        return null;
    }
}
